package edu.hnu.cloudflight.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import edu.hnu.cloudflight.android.adapter.FlightNumberListAdapter;
import edu.hnu.cloudflight.android.domain.Flight;
import edu.hnu.cloudflight.android.server.FlightDBServer;
import edu.hnu.cloudflight.android.util.StringUtil;
import edu.hnu.cloudflight.android.util.ToastUtil;

public class FlightCollectActivity extends Fragment {

	@SuppressWarnings("unused")
	private static final String TAG = "FlightCollectActivity";

	private ListView listView;

	private List<Flight> collectList;

	private FlightNumberListAdapter adapter;

	private FlightDBServer flightDBServer;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_flightcollect, container, false);
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		listView = (ListView) getView().findViewById(R.id.flightcollectlist);
		
		if (MyApplication.appToken == null || "".equals(MyApplication.appToken)) {
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

			Map<String, Object> temp = new HashMap<String, Object>();
			temp.put("info", StringUtil.getStringFromRes(R.string.flightcollect_tos_login));
			data.add(temp);

			SimpleAdapter infoAdapter = new SimpleAdapter(getActivity(), data, R.layout.listview_onetextview, new String[] { "info" },
					new int[] { R.id.onetextview_info });
			listView.setAdapter(infoAdapter);
			return ;
			
		}
		flightDBServer = FlightDBServer.getInstant(getActivity());
		

		// 解决Listview上下拖动时
		listView.setCacheColorHint(Color.TRANSPARENT);

		collectList = flightDBServer.getAllFlights();

		adapter = new FlightNumberListAdapter(getActivity(), collectList, true);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(), FlightNumberResultActivity.class);
				intent.setAction(FlightNumberResultActivity.ACTION_FROM_COLLECT);
				intent.putExtra("flightNum", collectList.get(position).name);
				startActivity(intent);
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

				final String flightnum = collectList.get(position).name;
				new AlertDialog.Builder(getActivity()).setMessage(R.string.flightcollect_diag_affirmdlecollect)
						.setPositiveButton(R.string.ok, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub

								flightDBServer.delFlightCollect(flightnum);
								collectList = flightDBServer.getAllFlights();
								adapter.setData(collectList);
								adapter.notifyDataSetChanged();
								ToastUtil.makeToastForShort(getActivity(), R.string.flightcollect_tos_delcollectsuccess);

							}
						}).setNegativeButton(R.string.cancel, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub

							}
						}).show();

				return false;
			}
		});
	}

}
